/*
 * Feb/13/2013
 * 
 * Group 2
 * ITCS 3146
 * 
 * Group Project
 * MULTICS Paging Algorithm
 * 
 */


package MULTICS;

import java.util.*;

// Restricted  page size to 1024 words( 1 physical address per word)
public class SegmentDescriptor 
{
	private int segmentLength;
	private int segmentNumber;
	private boolean pageStatus = false;
	private String protection = "";
	private PageDescriptor[] pageTable = null;

	// Creates page table, assigns protection, and sets segment number
	SegmentDescriptor(int segNumb) 
	{
		// Create protection for segment
		Random generator = new Random();

		// Protection level generation
		int randNumb = generator.nextInt(7)+1;
	    protection = Integer.toBinaryString(randNumb);

		if (randNumb < 2)
			protection = "00" + protection;
		else if (randNumb < 4)
			protection = "0" + protection;

		
		segmentNumber = segNumb;
		
		// Create the page table for the segment creates 1-7 pages
		segmentLength = generator.nextInt(7) + 1;	
		pageTable = new PageDescriptor[segmentLength];

		for (int i = 0; i < segmentLength; i++) 
		{
			pageTable[i] = new PageDescriptor(i, protection);
		}
	}
	
	// Returns segment number
	// Used in PageFaultHandler
	public int returnSegNumb()
	{
		return segmentNumber;
	}

	// Updates pageStatus
	public void updatePageStat(boolean status) 
	{
		pageStatus = status;
	}

	// Determines if segment is paged
	public boolean Paged() 
	{
		return pageStatus;
	}

	// Number of pages in segment's virtual address space
	public int numberOfPages() 
	{
		return segmentLength;
	}

	// Returns page table
	public PageDescriptor[] returnPageTable() 
	{
		return pageTable;
	}

	// Returns protection bits
	public String returnProtection() 
	{
		return protection;
	}


}
